package com.szosen.jkzj.activity;

import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;

import com.szosen.device.Glucose.BluetoothLeService;
import com.szosen.device.Glucose.GLUSanNuo_Activity;
import com.szosen.device.Glucose.GLU_Activity;
import com.szosen.device.Temp.TiwenActivity;
import com.szosen.device.Xuezhi.XuezhiActivity;
import com.szosen.device.jianhuyi.JianHuYiActivity_jieyang;
import com.szosen.jkzj.R;
import com.szosen.json.database.ArchiveBean;
import com.szosen.json.database.JSONDBService;
import com.szosen.measurement.MeasureECGActivity;
import com.szosen.measurement.MeasureUrine;
import com.szosen.util.SharedpreferenceUtil;
import com.szosen.util.ToastTool;
import com.szosen.util.TwoCloumAdapter;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class UserDetail extends BaseActivity {

	private static final int COVER_REUSLT = 0;
	private static final int PERSONAL_INFO = 1;
	private static final int HEALTH_CHECK = 2;

	// private MyProgressDialog dialog;
	private BluetoothAdapter mBtAdapter;
	/***
	 * 判断是否正在测量血压的标记
	 */
	public static boolean isMeasureBP = false;
	public static final String str_getBp = "血压数据未获取，请获取后再试！";
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.health);
		super.setTitleContent("【健康档案】");
		initMeasureBtn();
		initView();
		mBtAdapter = BluetoothAdapter.getDefaultAdapter();
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (mBtAdapter.getState() == BluetoothAdapter.STATE_OFF) {
			mBtAdapter.enable();// 打开蓝牙
		}
		getDateFromDataBase();
		try {
			Intent gattServiceIntent = new Intent(UserDetail.this,
					BluetoothLeService.class);
			// stopService(gattServiceIntent);
			gattServiceIntent = new Intent(UserDetail.this,
					com.szosen.device.Temp.BluetoothLeService.class);
			// stopService(gattServiceIntent);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	protected void onStart() {
		super.onStart();
		getDateFromDataBase();

	};

	Button measure_record;// 测量记录
	// Button health_measure;// 健康测量
	Button gluButton;// 血糖测量
	Button xuezhiButton;// 血脂测量
	Button boButton;// 血氧测量
	Button bpButton;// 血压测量
	Button tempButton;// 体温按钮
	Button uaButton;// 尿酸按钮
	Button cholButton;// 总胆固醇按钮
	Button urineButton;// 尿液分析按钮
	Button measureEcgButton;// 十二导心电
	Button shengongneng, xuezhi, bchao;//
	OnClickListener onClickListener;
	private ListView list;
	private TwoCloumAdapter adapter;

	private void initMeasureBtn() {
		boButton = (Button) findViewById(R.id.health_btn_measurexueyang);// 血氧
		tempButton = (Button) this.findViewById(R.id.health_btn_measureerwen);// 体温
		bpButton = (Button) findViewById(R.id.health_btn_measurexueya);// 血压
		measureEcgButton = (Button) findViewById(R.id.health_btn_measureecg);
		measure_record = (Button) findViewById(R.id.health_btn_measurerecord);
		urineButton = (Button) findViewById(R.id.health_btn_measureniaoye);
		gluButton = (Button) findViewById(R.id.health_btn_measurexuetang);
		xuezhiButton = (Button) findViewById(R.id.health_btn_measurexuezhi);
		measure_record.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// 跳转到测量历史记录
				Intent intent = new Intent(UserDetail.this, LocalRecord.class);
				startActivity(intent);
			}
		});
		OnClickListener listener = new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = null;
				if (v == bpButton || v == boButton) {
					intent = new Intent(UserDetail.this, JianHuYiActivity_jieyang.class);
					if (v == boButton) {
						if (isMeasureBP) {
							ToastTool.showShort(context, str_getBp);
							return;
						}
						intent.putExtra("from", 2);
					} else if (v == bpButton) {
						intent.putExtra("from", 1);
					}
				}
				if (v == tempButton) {
					// intent = new Intent(UserDetail.this,
					// TiwenActivity.class);
					intent = new Intent(UserDetail.this, TiwenActivity.class);
					intent.putExtra("temp", "fag");
				}
				if (v == measureEcgButton) {
					if (isMeasureBP) {
						ToastTool.showShort(context, str_getBp);
						return;
					}
					intent = new Intent(UserDetail.this,
							MeasureECGActivity.class);
				}
				if (v == urineButton) {
					intent = new Intent(UserDetail.this, MeasureUrine.class);
				}
				if (v == gluButton) {
					boolean useSannuo = SharedpreferenceUtil.getUseSannuo(context);
					if (useSannuo){
						intent = new Intent(UserDetail.this, GLUSanNuo_Activity.class);
					}else{
						intent = new Intent(UserDetail.this, GLU_Activity.class);
					}
				}
				if (v == xuezhiButton) {
					intent = new Intent(UserDetail.this, XuezhiActivity.class);
				}
				if (intent != null) {
					startActivity(intent);
				}
			}
		};
		boButton.setOnClickListener(listener);
		tempButton.setOnClickListener(listener);
		bpButton.setOnClickListener(listener);
		measureEcgButton.setOnClickListener(listener);
		urineButton.setOnClickListener(listener);
		gluButton.setOnClickListener(listener);
		xuezhiButton.setOnClickListener(listener);
	}

	/** 健康体检表43 */
	protected void getHealthCheckTable() {
		JSONObject json = new JSONObject();
		try {
		} catch (Exception e) {
			Log.i("getHealthCheckTable", e.toString());
		}
	}

	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
		super.onActivityResult(arg0, arg1, arg2);
		getDateFromDataBase();
	}

	private void getDateFromDataBase() {
		String timejkda = BaseActivity.getJKDATime();
		ArchiveBean queryBean = new ArchiveBean();
		queryBean.setTime(timejkda);
		ArchiveBean bean = BaseActivity.getArchiveBean();
		if (!TextUtils.isEmpty(timejkda)) {
			List<ArchiveBean> lists = (List<ArchiveBean>) JSONDBService
					.queryByKey(queryBean);
			if (lists.size() > 0) {
				bean = lists.get(0);
			}
		}
		List<String[]> data = new ArrayList<String[]>();
		if (bean == null)// 用户不存在,异常情况.
			finish();
		// Log.i("user.getName()", (bean.getName() == null) + "");
		data.add(new String[] { "姓名", bean.getName() });
		data.add(new String[] { "卡号", bean.getIdcard() });
		// data.add(new String[] { "用户id", bean.getTime() });
		data.add(new String[] { "出生日期", bean.getBirthday() });
		String sex = bean.getSex();
		data.add(new String[] { "性别", sex });
		// data.add(new String[] { "昵称", bean. });
		// data.add(new String[] { "编号", bean.getCheckid() });
		// data.add(new String[] { "机构id", bean.get });
		// data.add(new String[] { "手机号码", bean.getPhoneNumber() });
		// data.add(new String[] { "民族",
		// NationUtil.getNationStr(bean.getNationCode()) });
		for (int i = 0; i < data.size(); i++) {
			String[] tmp = data.get(i);
			for (int j = 0; j < tmp.length; j++) {
				if (tmp[j] == null || tmp[j].length() == 0
						|| "null".equals(tmp[j]))
					data.get(i)[j] = "无";
			}
		}
		adapter = new TwoCloumAdapter(this, data);
		list.setAdapter(adapter);
		adapter.notifyDataSetChanged();
	}

	private void initView() {
		list = (ListView) findViewById(R.id.user_detail_list);
		list.setEnabled(false);

	}

	/**
	 * 跳转进入用户档案
	 *
	 * @param view
	 */

	public void healthArchive(View view) {
		Intent intent = new Intent(this, JKDAActivity.class);
		intent.putExtra("time", BaseActivity.getJKDATime());
		startActivity(intent);
	}

	/***
	 * 跳转到体检的列表
	 *
	 * @param view
	 */
	public void goTojktjList(View view) {
		Intent intent = new Intent(this, HealthCheckList.class);
		startActivity(intent);

	}

}
